home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / thesrc10.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1992-08-11  |  57KB  |  1,732 lines

  1. /***********************************************************************/
  2. /* UTIL.C - Utility routines                                           */
  3. /***********************************************************************/
  4. /*
  5.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  6.  * Copyright (C) 1991,1992 Mark Hessling
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  29.  * This software is going to be maintained and enhanced as deemed
  30.  * necessary by the community.
  31.  *
  32.  * Mark Hessling                     email: M.Hessling@itc.gu.edu.au
  33.  * 36 David Road                     Phone: +61 7 849 7731
  34.  * Holland Park                      Fax:   +61 7 875 7877
  35.  * QLD 4121
  36.  * Australia
  37.  */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include "the.h"
  41.  
  42. /*--------------------------- common data -----------------------------*/
  43. #define MAX_RECV 20
  44.  static unsigned char *recv[MAX_RECV];
  45.  static int recv_len[MAX_RECV];
  46.  static int add_recv=(-1);
  47.  static int retr_recv=(-1);
  48.  static int num_recv=0;
  49. /*-------------------------- external data ----------------------------*/
  50. extern LINE *next_line,*curr_line;
  51. extern VIEW_DETAILS *vd_current,*vd_first,*vd_mark;
  52. extern char current_screen;
  53. extern SCREEN_DETAILS screen[MAX_SCREENS];        /* screen structures */
  54. extern char number_of_views;                   /* number of open files */
  55. extern char current_file;                   /* pointer to current file */
  56. extern WINDOW *foot,*error_window;
  57. extern char error_on_screen;
  58. extern unsigned char *rec;
  59. extern unsigned short rec_len;
  60. extern unsigned char mode_insert;        /* defines insert mode toggle */
  61. extern unsigned char in_profile;    /* indicates if processing profile */
  62. extern unsigned char temp_cmd[150];
  63. /*-------------------------- function definitions ---------------------*/
  64. #ifdef PROTO
  65. void post_process_line(long line_number);
  66. void pre_process_line(long line_number);
  67. #else
  68. void post_process_line();
  69. void pre_process_line();
  70. #endif
  71. /*man***************************************************************************
  72. NAME
  73.      memrevne - search buffer reversed for NOT character
  74.  
  75. SYNOPSIS
  76.      #include "the.h"
  77.  
  78.      short memrevne(buffer,known_char,max_len)
  79.      char *buffer;
  80.      unsigned char known_char;
  81.      short max_len;
  82.  
  83. DESCRIPTION
  84.      The memrevne function searches the buffer from the right for first
  85.      character NOT equal to the supplied character.
  86.  
  87. RETURN VALUE
  88.      If successful, returns the position of first NON-matching character
  89.      or (-1) if unsuccessful.
  90.  
  91. SEE ALSO
  92.      strzrevne, strzne
  93. *******************************************************************************/
  94. #ifdef PROTO
  95. short memrevne(char *buffer,unsigned char known_char,short max_len)
  96. #else
  97. short memrevne(buffer,known_char,max_len)
  98. char *buffer;
  99. unsigned char known_char;
  100. short max_len;
  101. #endif
  102. {
  103. /*--------------------------- local data ------------------------------*/
  104.  register int len=max_len;
  105. /*--------------------------- processing ------------------------------*/
  106.  for (--len; len>=0 && buffer[len]==known_char; len--);
  107.  return(len);
  108. }
  109. /*man***************************************************************************
  110. NAME
  111.      meminschr - insert character into buffer
  112.  
  113. SYNOPSIS
  114.      #include "the.h"
  115.  
  116.      char *meminschr(buffer,chr,location,max_length,curr_length)
  117.      char *buffer;
  118.      unsigned char chr;
  119.      short location,max_length,curr_length;
  120.  
  121. DESCRIPTION
  122.      The meminschr inserts the supplied 'chr' into the buffer 'buffer'
  123.      before the 'location' specified. 'location' is an offset (0 based)
  124.      from the start of 'buffer'.
  125.      The 'buffer' will not be allowed to have more than 'max_length'
  126.      characters, so if the insertion of the character causes the
  127.      'max_length' to be exceeded, the last character of 'buffer' will
  128.      be lost.
  129.  
  130. RETURN VALUE
  131.      A pointer to the same 'buffer' as was supplied.
  132.  
  133. SEE ALSO
  134.     meminsstr, memdelchr
  135.  
  136. *******************************************************************************/
  137. #ifdef PROTO
  138. char *meminschr(char *buffer,unsigned char chr,short location,
  139.                 short max_length,short curr_length)
  140. #else
  141. char *meminschr(buffer,chr,location,max_length,curr_length)
  142. char *buffer;
  143. unsigned char chr;
  144. short location,max_length,curr_length;
  145. #endif
  146. {
  147. /*--------------------------- local data ------------------------------*/
  148.  register int i;
  149. /*--------------------------- processing ------------------------------*/
  150.  for (i=curr_length;i > location;i--)
  151.      if (i < max_length)
  152.        buffer[i] = buffer[i-1];
  153.  if (location < max_length)
  154.     buffer[location] = chr;
  155.  return(buffer);
  156. }
  157. /*man***************************************************************************
  158. NAME
  159.      meminsmem - insert string into buffer
  160.  
  161. SYNOPSIS
  162.      #include "the.h"
  163.  
  164.      char *meminsmem(buffer,str,location,max_length,curr_length)
  165.      char *buffer;
  166.      char *str;
  167.      short location,max_length,curr_length;
  168.  
  169. DESCRIPTION
  170.      The meminsmem function inserts the supplied 'str' into the buffer 'buffer'
  171.      before the 'location' specified. 'location' is an offset (0 based)
  172.      from the start of 'buffer'.
  173.      The 'buffer' will not be allowed to have more than 'max_length'
  174.      characters, so if the insertion of the string causes the
  175.      'max_length' to be exceeded, the last character(s) of 'buffer' will
  176.      be lost.
  177.  
  178. RETURN VALUE
  179.      A pointer to the same 'buffer' as was supplied.
  180.  
  181. SEE ALSO
  182.     meminschr
  183.  
  184. *******************************************************************************/
  185. #ifdef PROTO
  186. char *meminsmem(char *buffer,char *str,short len,short location,
  187.                 short max_length,short curr_length)
  188. #else
  189. char *meminsmem(buffer,str,len,location,max_length,curr_length)
  190. char *buffer,*str;
  191. short len,location,max_length,curr_length;
  192. #endif
  193. {
  194. /*--------------------------- local data ------------------------------*/
  195.  register int i;
  196. /*--------------------------- processing ------------------------------*/
  197.  for (i=curr_length;i > location;i--)
  198.      if (i+len-1 < max_length)
  199.        buffer[i+len-1] = buffer[i-1];
  200.  for (i=0;i<len;i++)
  201.      if (location+i < max_length)
  202.        buffer[location+i] = str[i];
  203.  return(buffer);
  204. }
  205. /*man***************************************************************************
  206. NAME
  207.      memdelchr - delete character(s) from buffer
  208.  
  209. SYNOPSIS
  210.      #include "the.h"
  211.  
  212.      char *memdelchr(buffer,location,curr_length,num_chars)
  213.      char *buffer;
  214.      short location,curr_length,num_chars;
  215.  
  216. DESCRIPTION
  217.      The memdelchr deletes the supplied number of characters from the
  218.      buffer starting at the 'location' specified. 'location' is an offset (0 based)
  219.      from the start of 'buffer'.
  220.      For each character deleted, what was the last character in buffer;
  221.      based on 'curr_length' will be replaced with a space.
  222.  
  223. RETURN VALUE
  224.      A pointer to the same 'buffer' as was supplied.
  225.  
  226. SEE ALSO
  227.     meminschr
  228.  
  229. *******************************************************************************/
  230. #ifdef PROTO
  231. char *memdelchr(char *buffer,short location,short curr_length,
  232.                 short num_chars)
  233. #else
  234. char *memdelchr(buffer,location,curr_length,num_chars)
  235. char *buffer;
  236. short location,curr_length,num_chars;
  237. #endif
  238. {
  239. /*--------------------------- local data ------------------------------*/
  240.  register int i;
  241. /*--------------------------- processing ------------------------------*/
  242.  for (i=location;i <curr_length;i++)
  243.      if (i+num_chars >= curr_length)
  244.         buffer[i] = ' ';
  245.       else
  246.         buffer[i] = buffer[i+num_chars];
  247.  return(buffer);
  248. }
  249. /*man***************************************************************************
  250. NAME
  251.      strzne - search string for NOT character
  252.  
  253. SYNOPSIS
  254.      #include "the.h"
  255.  
  256.      short strzne(str,chr)
  257.      char *str;
  258.      unsigned char ch;
  259.  
  260. DESCRIPTION
  261.      The strzne function searches the string from the left for the first
  262.      character NOT equal to the supplied character.
  263.  
  264. RETURN VALUE
  265.      If successful, returns the position of first NON-matching character
  266.      or (-1) if unsuccessful.
  267.  
  268. SEE ALSO
  269.      strzrevne, memrevne
  270. *******************************************************************************/
  271. #ifdef PROTO
  272. short strzne(char *str,unsigned char ch)
  273. #else
  274. short strzne(str,ch)
  275. char *str;
  276. unsigned char ch;
  277. #endif
  278. {
  279. /*--------------------------- local data ------------------------------*/
  280.  register int len;
  281.  register int  i = 0;
  282. /*--------------------------- processing ------------------------------*/
  283.  len = strlen(str);
  284.  for (; i<len && str[i]==ch; i++);
  285.  if (i>=len)
  286.     i = (-1);
  287.  return(i);
  288. }
  289. /*man***************************************************************************
  290. NAME
  291.      memne - search buffer for NOT character
  292.  
  293. SYNOPSIS
  294.      #include "the.h"
  295.  
  296.      short memne(buffer,chr,length)
  297.      unsigned char *buffer;
  298.      unsigned char chr;
  299.      short length;
  300.  
  301. DESCRIPTION
  302.      The memne function searches the buffer from the left for the first
  303.      character NOT equal to the supplied character.
  304.  
  305. RETURN VALUE
  306.      If successful, returns the position of first NON-matching character
  307.      or (-1) if unsuccessful.
  308.  
  309. SEE ALSO
  310.      strzrevne, memrevne, strzne
  311. *******************************************************************************/
  312. #ifdef PROTO
  313. short memne(unsigned char *buffer,unsigned char chr,short length)
  314. #else
  315. short memne(buffer,chr,length)
  316. unsigned char *buffer;
  317. unsigned char chr;
  318. short length;
  319. #endif
  320. {
  321. /*--------------------------- local data ------------------------------*/
  322.  register int  i = 0;
  323. /*--------------------------- processing ------------------------------*/
  324.  for (; i<length && buffer[i]==chr; i++);
  325.  if (i>=length)
  326.     i = (-1);
  327.  return(i);
  328. }
  329. /*man***************************************************************************
  330. NAME
  331.      strzrevne - search string reversed for NOT character
  332.  
  333. SYNOPSIS
  334.      #include "the.h"
  335.  
  336.      short strzrevne(str,chr)
  337.      char *str;
  338.      unsigned char ch;
  339.  
  340. DESCRIPTION
  341.      The strzrevne function searches the string from the right for the
  342.      first character NOT equal to the supplied character.
  343.  
  344. RETURN VALUE
  345.      If successful, returns the position of first NON-matching character
  346.      or (-1) if unsuccessful.
  347.  
  348. SEE ALSO
  349.      strzne, memrevne
  350. *******************************************************************************/
  351. #ifdef PROTO
  352. short strzrevne(char *str,unsigned char ch)
  353. #else
  354. short strzrevne(str,ch)
  355. char *str;
  356. unsigned char ch;
  357. #endif
  358. {
  359. /*--------------------------- local data ------------------------------*/
  360.  register int len;
  361. /*--------------------------- processing ------------------------------*/
  362.  len = strlen(str);
  363.  for (--len; len>=0 && str[len]==ch; len--);
  364.  return(len);
  365. }
  366. /*man***************************************************************************
  367. NAME
  368.      strzreveq - search string reversed for character
  369.  
  370. SYNOPSIS
  371.      short strzreveq(str,chr)
  372.      char *str;
  373.      unsigned char ch;
  374.  
  375. DESCRIPTION
  376.      The strzreveq function searches the string from the right for the
  377.      first character equal to the supplied character.
  378.  
  379. RETURN VALUE
  380.      If successful, returns the position of first matching character
  381.      or (-1) if unsuccessful.
  382.  
  383. SEE ALSO
  384.      strzrevne
  385. *******************************************************************************/
  386. #ifdef PROTO
  387. short strzreveq(unsigned char *str,unsigned char ch)
  388. #else
  389. short strzreveq(str,ch)
  390. unsigned char *str,ch;
  391. #endif
  392. /***********************************************************************/
  393. {
  394. /*--------------------------- local data ------------------------------*/
  395.  register short len;
  396. /*--------------------------- processing ------------------------------*/
  397.  len = strlen(str);
  398.  for (--len; len>=0 && str[len]!=ch; len--);
  399.  return(len);
  400. }
  401. /*man***************************************************************************
  402. NAME
  403.      strtrunc - truncate trailing spaces from string
  404.  
  405. SYNOPSIS
  406.      #include "the.h"
  407.  
  408.      char *strtrunc(string)
  409.      char *string;
  410.  
  411. DESCRIPTION
  412.      The strtrunc truncates all trailing spaces from the supplied string.
  413.  
  414. RETURN VALUE
  415.      A pointer to the original string, now truncated.
  416.  
  417. SEE ALSO
  418.  
  419. *******************************************************************************/
  420. #ifdef PROTO
  421. char *strtrunc(char *string)
  422. #else
  423. char *strtrunc(string)
  424. char *string;
  425. #endif
  426. {
  427. /*--------------------------- local data ------------------------------*/
  428.  register short i;
  429. /*--------------------------- processing ------------------------------*/
  430.  i = strzrevne(string,' ');
  431.  if (i == (-1))
  432.     strcpy(string,"");
  433.  else
  434.     *(string+i+1) = '\0';
  435.  return(string);
  436. }
  437. /***********************************************************************/
  438. #ifdef PROTO
  439. short mempos(unsigned char *haystack,short hay_len,
  440.              unsigned char *needle,short nee_len)
  441. #else
  442. short mempos(haystack,hay_len,needle,nee_len)
  443. unsigned char *haystack,*needle;
  444. short hay_len,nee_len;
  445. #endif
  446. /***********************************************************************/
  447. /* Function  : Finds a needle in a haystack in a memory array.         */
  448. /* Parameters: haystack - where to find the needle                     */
  449. /*             hay_len  - the length of the haystack                   */
  450. /*             needle   - the needle to find                           */
  451. /*             nee_len  - the length of the needle                     */
  452. /* Return    : position in haystack (0 based) or (-1) if no needle     */
  453. /***********************************************************************/
  454. {
  455. /*--------------------------- local data ------------------------------*/
  456.  register short i;
  457. /*--------------------------- processing ------------------------------*/
  458.  for (i=0;i<(hay_len-nee_len);i++)
  459.      if (memcmp(haystack+i,needle,nee_len) == 0)
  460.         return(i);
  461.  return(-1);
  462. }
  463. /***********************************************************************/
  464. #ifdef PROTO
  465. short memposi(unsigned char *haystack,short hay_len,
  466.               unsigned char *needle,short nee_len)
  467. #else
  468. short memposi(haystack,hay_len,needle,nee_len)
  469. unsigned char *haystack,*needle;
  470. short hay_len,nee_len;
  471. #endif
  472. /***********************************************************************/
  473. /* Function  : Finds a needle in a haystack in a memory array;         */
  474. /*             case insensitive.                                       */
  475. /* Parameters: haystack - where to find the needle                     */
  476. /*             hay_len  - the length of the haystack                   */
  477. /*             needle   - the needle to find                           */
  478. /*             nee_len  - the length of the needle                     */
  479. /* Return    : position in haystack (0 based) or (-1) if no needle     */
  480. /***********************************************************************/
  481. {
  482. /*--------------------------- local data ------------------------------*/
  483.  register short i;
  484. /*--------------------------- processing ------------------------------*/
  485.  for (i=0;i<(hay_len-nee_len+1);i++)
  486.      if (memcmpi(haystack+i,needle,nee_len) == 0)
  487.         return(i);
  488.  return(-1);
  489. }
  490. /***********************************************************************/
  491. #ifdef PROTO
  492. short memcmpi(unsigned char *buf1,unsigned char *buf2,short len)
  493. #else
  494. short memcmpi(buf1,buf2,len)
  495. unsigned char *buf1,*buf2;
  496. short len;
  497. #endif
  498. /***********************************************************************/
  499. /* Function  : Compares two memory buffers for equality;               */
  500. /*             case insensitive. Same as memicmp() Microsoft C.        */
  501. /* Parameters: buf1     - first buffer                                 */
  502. /*             buf2     - second buffer                                */
  503. /*             len      - number of characters to compare.             */
  504. /* Return    : <0 if buf1 < buf2,                                      */
  505. /*             =0 if buf1 = buf2,                                      */
  506. /*             >0 if buf1 > buf2,                                      */
  507. /***********************************************************************/
  508. {
  509. /*--------------------------- local data ------------------------------*/
  510.  register short i;
  511. /*--------------------------- processing ------------------------------*/
  512. #ifdef SUN
  513.  for(i=0;i<len;i++)
  514.    {
  515.     unsigned char c1,c2;
  516.     if (isupper(*buf1))
  517.        c1 = tolower(*buf1);
  518.     else
  519.        c1 = *buf1;
  520.     if (isupper(*buf2))
  521.        c2 = tolower(*buf2);
  522.     else
  523.        c2 = *buf2;
  524.     if (c1 != c2)
  525.        return(c1-c2);
  526.     ++buf1;
  527.     ++buf2;
  528.    }
  529. #else
  530.  for(i=0;i<len;i++)
  531.    {
  532.     if (tolower(*buf1) != tolower(*buf2))
  533.        return tolower(*buf1) - tolower(*buf2);
  534.     ++buf1;
  535.     ++buf2;
  536.    }
  537. #endif
  538.  return(0);
  539. }
  540. /*man***************************************************************************
  541. NAME
  542.      equal - determine if strings are equal up to specified length
  543.  
  544. SYNOPSIS
  545.      unsigned short equal(con,str,min_len)
  546.      char *con,*str;
  547.      short min_len;
  548.  
  549. DESCRIPTION
  550.      The equal function determines if a two strings are equal, irrespective
  551.      of case, up to the length of the second string. The length of the
  552.      string must be greater than or equal to the specified minimum length.
  553.  
  554. RETURN VALUE
  555.      If 'equal' TRUE else FALSE.
  556. *******************************************************************************/
  557. #ifdef PROTO
  558. unsigned short equal(unsigned char *con,unsigned char *str,short min_len)
  559. #else
  560. unsigned short equal(con,str,min_len)
  561. unsigned char *con,*str;
  562. short min_len;
  563. #endif
  564. {
  565. /*--------------------------- local data ------------------------------*/
  566. /*--------------------------- processing ------------------------------*/
  567. #ifdef TRACE
  568.  trace_function("util.c:    equal");
  569. #endif
  570.  if (memcmpi(con,str,min(strlen(str),strlen(con))) == 0
  571.  &&  strlen(str) >= min_len
  572.  &&  strlen(con) >= strlen(str))
  573.    {
  574. #ifdef TRACE
  575.     trace_return();
  576. #endif
  577.     return(TRUE);
  578.    }
  579. #ifdef TRACE
  580.  trace_return();
  581. #endif
  582.  return(FALSE);
  583. }
  584. /***********************************************************************/
  585. #ifdef PROTO
  586. int valid_integer(unsigned char *str)
  587. #else
  588. int valid_integer(str)
  589. unsigned char *str;
  590. #endif
  591. /***********************************************************************/
  592. /* Function  : Checks that string contains only 0-9,- or +.            */
  593. /* Parameters: *str     - string to be checked                         */
  594. /* Return    : YES or NO                                               */
  595. /***********************************************************************/
  596. {
  597. /*--------------------------- local data ------------------------------*/
  598.    register int  i;
  599.    int num_signs=0;
  600. /*--------------------------- processing ------------------------------*/
  601. #ifdef TRACE
  602.  trace_function("util.c:    valid_integer");
  603. #endif
  604.  for (i=0; i<strlen(str); i++)
  605.     {
  606.      if (*(str+i) == '-' || *(str+i) == '+')
  607.         num_signs++;
  608.      else
  609.         if (!isdigit(*(str+i)))
  610.           {
  611. #ifdef TRACE
  612.            trace_return();
  613. #endif
  614.            return(NO);
  615.           }
  616.     }
  617.  if (num_signs > 1)
  618.    {
  619. #ifdef TRACE
  620.     trace_return();
  621. #endif
  622.     return(NO);
  623.    }
  624. #ifdef TRACE
  625.  trace_return();
  626. #endif
  627.  return(YES);
  628. }
  629. /***********************************************************************/
  630. #ifdef PROTO
  631. int valid_positive_integer(unsigned char *str)
  632. #else
  633. int valid_positive_integer(str)
  634. unsigned char *str;
  635. #endif
  636. /***********************************************************************/
  637. /* Function  : Checks that string contains only 0-9, or +.             */
  638. /* Parameters: *str     - string to be checked                         */
  639. /* Return    : YES or NO                                               */
  640. /***********************************************************************/
  641. {
  642. /*--------------------------- local data ------------------------------*/
  643.    register int  i;
  644.    int num_signs=0;
  645. /*--------------------------- processing ------------------------------*/
  646. #ifdef TRACE
  647.  trace_function("util.c:    valid_positive_integer");
  648. #endif
  649.  for (i=0; i<strlen(str); i++)
  650.     {
  651.      if (*(str+i) == '+')
  652.         num_signs++;
  653.      else
  654.         if (!isdigit(*(str+i)))
  655.           {
  656. #ifdef TRACE
  657.            trace_return();
  658. #endif
  659.            return(NO);
  660.           }
  661.     }
  662.  if (num_signs > 1)
  663.    {
  664. #ifdef TRACE
  665.     trace_return();
  666. #endif
  667.     return(NO);
  668.    }
  669. #ifdef TRACE
  670.  trace_return();
  671. #endif
  672.  return(YES);
  673. }
  674. /***********************************************************************/
  675. #ifdef PROTO
  676. int strzeq(unsigned char *str,unsigned char ch)
  677. #else
  678. int strzeq(str,ch)
  679. unsigned char *str;
  680. char ch;
  681. #endif
  682. /***********************************************************************/
  683. /* Function  : Locate in ASCIIZ string, character                      */
  684. /* Parameters: *str     - string to be searched                        */
  685. /*             ch       - character to be searched for                 */
  686. /* Return    : position in string of character - (-1) if not found     */
  687. /***********************************************************************/
  688. {
  689. /*--------------------------- local data ------------------------------*/
  690.  register int len;
  691.  register int  i = 0;
  692. /*--------------------------- processing ------------------------------*/
  693.  len = strlen(str);
  694.  for (; i<len && str[i]!=ch; i++);
  695.  if (i>=len)
  696.     i = (-1);
  697.  return(i);
  698. }
  699.  
  700. /***********************************************************************/
  701. #ifdef PROTO
  702. LINE *ll_add(LINE *first,LINE *curr,unsigned short size)
  703. #else
  704. LINE *ll_add(first,curr,size)
  705. LINE *first;
  706. LINE *curr;
  707. unsigned short size;
  708. #endif
  709. /***********************************************************************/
  710. /* Adds a LINE to the current linked list after the current member.    */
  711. /* PARAMETERS:                                                         */
  712. /* first      - pointer to first LINE in linked list                   */
  713. /* curr       - pointer to current LINE in linked list                 */
  714. /* size       - size of a LINE item                                    */
  715. /* RETURN:    - pointer to next LINE item                              */
  716. /***********************************************************************/
  717. {
  718. /*--------------------------- local data ------------------------------*/
  719.  LINE *next=NULL;
  720. /*--------------------------- processing ------------------------------*/
  721. #ifdef TRACE
  722.  trace_function("util.c:    ll_add");
  723. #endif
  724.  
  725.  if ((next=(LINE *)memMalloc(size,"ll_add LINE *")) != NULL)
  726.     {
  727.      if (curr == NULL)
  728.         {
  729.          first = next;
  730.          next->next = NULL;
  731.         }
  732.      else
  733.         {
  734.          if (curr->next != NULL)
  735.             curr->next->prev = next;
  736.          next->next = curr->next;
  737.          curr->next = next;
  738.         }
  739.      next->prev = curr;
  740.     }
  741. #ifdef TRACE
  742.  trace_return();
  743. #endif
  744.  return(next);
  745. }
  746. /***********************************************************************/
  747. #ifdef PROTO
  748. LINE *ll_del(LINE *first,LINE *curr,char direction)
  749. #else
  750. LINE *ll_del(first,curr,direction)
  751. LINE *first;
  752. LINE *curr;
  753. char direction;
  754. #endif
  755. /***********************************************************************/
  756. {
  757. /*--------------------------- local data ------------------------------*/
  758.  LINE *new_curr;
  759. /*--------------------------- processing ------------------------------*/
  760. #ifdef TRACE
  761.  trace_function("util.c:    ll_del");
  762. #endif
  763. /*---------------------------------------------------------------------*/
  764. /* Delete the only record                                              */
  765. /*---------------------------------------------------------------------*/
  766.  
  767.  if (curr->prev == NULL && curr->next == NULL)
  768.     {
  769.      memFree((void *)curr->line,curr->line);
  770.      memFree((void *)curr,"ll_del curr only");
  771. #ifdef TRACE
  772.      trace_return();
  773. #endif
  774.      return(NULL);
  775.     }
  776. /*---------------------------------------------------------------------*/
  777. /* Delete the first record                                             */
  778. /*---------------------------------------------------------------------*/
  779.  
  780.  if (curr->prev == NULL)
  781.     {
  782.      curr->next->prev = NULL;
  783.      new_curr = first = curr->next;
  784.      memFree((void *)curr->line,curr->line);
  785.      memFree((void *)curr,"ll_del curr first");
  786.      curr = new_curr;
  787. #ifdef TRACE
  788.      trace_return();
  789. #endif
  790.      return(curr);
  791.     }
  792. /*---------------------------------------------------------------------*/
  793. /* Delete the last  record                                             */
  794. /*---------------------------------------------------------------------*/
  795.  
  796.  if (curr->next == NULL)
  797.     {
  798.      curr->prev->next = NULL;
  799.      new_curr = curr->prev;
  800.      memFree((void *)curr->line,curr->line);
  801.      memFree((void *)curr,"ll_del curr last");
  802.      curr = new_curr;
  803. #ifdef TRACE
  804.      trace_return();
  805. #endif
  806.      return(curr);
  807.     }
  808.  
  809. /*---------------------------------------------------------------------*/
  810. /* All others                                                          */
  811. /*---------------------------------------------------------------------*/
  812.  curr->prev->next = curr->next;
  813.  curr->next->prev = curr->prev;
  814.  if (direction == DIRECTION_FORWARD)
  815.    new_curr = curr->next;
  816.  else
  817.    new_curr = curr->prev;
  818.  
  819.  memFree((void *)curr->line,curr->line);
  820.  memFree((void *)curr,"ll_del curr");
  821.  curr = new_curr;
  822. #ifdef TRACE
  823.  trace_return();
  824. #endif
  825.  return(curr);
  826. }
  827. /***********************************************************************/
  828. #ifdef PROTO
  829. void ll_free(LINE *first)
  830. #else
  831. void ll_free(first)
  832. LINE *first;
  833. #endif
  834. /***********************************************************************/
  835. /* Free up all allocated memory until the last item in the linked-list */
  836. /* PARAMETERS:                                                         */
  837. /* first      - pointer to first line for the file                     */
  838. /* RETURN:    - void                                                   */
  839. /***********************************************************************/
  840. {
  841. /*--------------------------- local data ------------------------------*/
  842.  LINE *curr;
  843. /*--------------------------- processing ------------------------------*/
  844. #ifdef TRACE
  845.  trace_function("util.c:    ll_free");
  846. #endif
  847.  curr = first;
  848.  while(curr != NULL)
  849.       {
  850.        memFree((void *)curr->line,curr->line);
  851.        memFree((void *)curr,"ll_free curr");
  852.        curr = curr->next;
  853.       }
  854. /*---------------------------------------------------------------------*/
  855. /* Free up the memory associated with the last item in the linked-list */
  856. /*---------------------------------------------------------------------*/
  857. /* memFree((void *)curr->line,curr->line);
  858.  memFree((void *)curr,"ll_free curr last"); */
  859. #ifdef TRACE
  860.  trace_return();
  861. #endif
  862.  return;
  863. }
  864. /***********************************************************************/
  865. #ifdef PROTO
  866. LINE *ll_find(LINE *first,unsigned long row)
  867. #else
  868. LINE *ll_find(first,row)
  869. LINE *first;
  870. unsigned long row;
  871. #endif
  872. /***********************************************************************/
  873. {
  874. /*--------------------------- local data ------------------------------*/
  875.  LINE *curr;
  876.  long i;
  877. /*--------------------------- processing ------------------------------*/
  878. #ifdef TRACE
  879.  trace_function("util.c:    ll_find");
  880. #endif
  881.  curr = first;
  882.  if (curr != NULL)
  883.     for(i=0L;i<row && curr->next != NULL; i++, curr=curr->next);
  884. #ifdef TRACE
  885.  trace_return();
  886. #endif
  887.  return(curr);
  888. }
  889. /***********************************************************************/
  890. #ifdef PROTO
  891. LINE *add_line(LINE *first,LINE *curr,unsigned char *line,
  892.                unsigned short len)
  893. #else
  894. LINE *add_line(first,curr,line,len)
  895. LINE *first;
  896. LINE *curr;
  897. unsigned char *line;
  898. unsigned short len;
  899. #endif
  900. /***********************************************************************/
  901. /* Adds a member of the linked list for the specified file containing  */
  902. /* the line contents and length.                                       */
  903. /* PARAMETERS:                                                         */
  904. /* first      - pointer to first line for the file                     */
  905. /* curr       - pointer to current line for the file                   */
  906. /* line       - contents of line to be added                           */
  907. /* len        - length of line to be added                             */
  908. /* RETURN:    - pointer to current item in linked list or NULL if error*/
  909. /***********************************************************************/
  910. {
  911. /*--------------------------- local data ------------------------------*/
  912. /*--------------------------- processing ------------------------------*/
  913. #ifdef TRACE
  914.  trace_function("util.c:    add_line");
  915. #endif
  916.  next_line = ll_add(first,curr,sizeof(LINE));
  917.  if (next_line == NULL)
  918.    {
  919. #ifdef TRACE
  920.     trace_return();
  921. #endif
  922.     return(NULL);
  923.    }
  924.  curr_line = next_line;
  925.  
  926.  curr_line->line = (unsigned char *)memMalloc((len+1)*sizeof(unsigned char),line);
  927.  if (curr_line->line == NULL)
  928.    {
  929. #ifdef TRACE
  930.     trace_return();
  931. #endif
  932.     return(NULL);
  933.    }
  934.  memcpy(curr_line->line,line,len);
  935.  *(curr_line->line+len) = '\0'; /* for functions that expect ASCIIZ string */
  936.  curr_line->length = len;
  937. #ifdef TRACE
  938.  trace_return();
  939. #endif
  940.  return(curr_line);
  941. }
  942. /***********************************************************************/
  943. #ifdef PROTO
  944. VIEW_DETAILS *vll_add(VIEW_DETAILS *first,VIEW_DETAILS *curr,unsigned short size)
  945. #else
  946. VIEW_DETAILS *vll_add(first,curr,size)
  947. VIEW_DETAILS *first;
  948. VIEW_DETAILS *curr;
  949. unsigned short size;
  950. #endif
  951. /***********************************************************************/
  952. /* Adds a VIEW_DETAILS to the current linked list after the current member.    */
  953. /* PARAMETERS:                                                         */
  954. /* first      - pointer to first VIEW_DETAILS in linked list                   */
  955. /* curr       - pointer to current VIEW_DETAILS in linked list                 */
  956. /* size       - size of a VIEW_DETAILS item                                    */
  957. /* RETURN:    - pointer to next VIEW_DETAILS item                              */
  958. /***********************************************************************/
  959. {
  960. /*--------------------------- local data ------------------------------*/
  961.  VIEW_DETAILS *next=(VIEW_DETAILS *)NULL;
  962. /*--------------------------- processing ------------------------------*/
  963. #ifdef TRACE
  964.  trace_function("util.c:    vll_add");
  965. #endif
  966.  
  967.  if ((next=(VIEW_DETAILS *)memMalloc(size,"vll_add VIEW_DETAILS *")) != (VIEW_DETAILS *)NULL)
  968.     {
  969.      if (curr == (VIEW_DETAILS *)NULL)
  970.         {
  971.          first = next;
  972.          next->next = (VIEW_DETAILS *)NULL;
  973.         }
  974.      else
  975.         {
  976.          if (curr->next != (VIEW_DETAILS *)NULL)
  977.             curr->next->prev = next;
  978.          next->next = curr->next;
  979.          curr->next = next;
  980.         }
  981.      next->prev = curr;
  982.     }
  983. #ifdef TRACE
  984.  trace_return();
  985. #endif
  986.  return(next);
  987. }
  988. /***********************************************************************/
  989. #ifdef PROTO
  990. VIEW_DETAILS *vll_del(VIEW_DETAILS *first,VIEW_DETAILS *curr,char direction)
  991. #else
  992. VIEW_DETAILS *vll_del(first,curr,direction)
  993. VIEW_DETAILS *first;
  994. VIEW_DETAILS *curr;
  995. char direction;
  996. #endif
  997. /***********************************************************************/
  998. {
  999. /*--------------------------- local data ------------------------------*/
  1000.  VIEW_DETAILS *new_curr;
  1001. /*--------------------------- processing ------------------------------*/
  1002. #ifdef TRACE
  1003.  trace_function("util.c:    vll_del");
  1004. #endif
  1005. /*---------------------------------------------------------------------*/
  1006. /* Delete the only record                                              */
  1007. /*---------------------------------------------------------------------*/
  1008.  
  1009.  if (curr->prev == (VIEW_DETAILS *)NULL && curr->next == (VIEW_DETAILS *)NULL)
  1010.     {
  1011.      memFree((void *)curr,"vll_del curr only");
  1012. #ifdef TRACE
  1013.      trace_return();
  1014. #endif
  1015.      first = curr = (VIEW_DETAILS *)NULL;
  1016.      return((VIEW_DETAILS *)NULL);
  1017.     }
  1018. /*---------------------------------------------------------------------*/
  1019. /* Delete the first record                                             */
  1020. /*---------------------------------------------------------------------*/
  1021.  
  1022.  if (curr->prev == (VIEW_DETAILS *)NULL)
  1023.     {
  1024.      curr->next->prev = (VIEW_DETAILS *)NULL;
  1025.      new_curr = vd_first = curr->next;
  1026.      memFree((void *)curr,"vll_del curr first");
  1027.      curr = new_curr;
  1028. #ifdef TRACE
  1029.      trace_return();
  1030. #endif
  1031.      return(curr);
  1032.     }
  1033. /*---------------------------------------------------------------------*/
  1034. /* Delete the last  record                                             */
  1035. /*---------------------------------------------------------------------*/
  1036.  
  1037.  if (curr->next == (VIEW_DETAILS *)NULL)
  1038.     {
  1039.      curr->prev->next = (VIEW_DETAILS *)NULL;
  1040.      new_curr = curr->prev;
  1041.      memFree((void *)curr,"vll_del curr last");
  1042.      curr = new_curr;
  1043. #ifdef TRACE
  1044.      trace_return();
  1045. #endif
  1046.      return(curr);
  1047.     }
  1048.  
  1049. /*---------------------------------------------------------------------*/
  1050. /* All others                                                          */
  1051. /*---------------------------------------------------------------------*/
  1052.  curr->prev->next = curr->next;
  1053.  curr->next->prev = curr->prev;
  1054.  if (direction == DIRECTION_FORWARD)
  1055.    new_curr = curr->next;
  1056.  else
  1057.    new_curr = curr->prev;
  1058.  
  1059.  memFree((void *)curr,"vll_del curr");
  1060.  curr = new_curr;
  1061. #ifdef TRACE
  1062.  trace_return();
  1063. #endif
  1064.  return(curr);
  1065. }
  1066. /***********************************************************************/
  1067. #ifdef PROTO
  1068. long find_string(long start_line,short *start_col,char *needle,
  1069.                  short len,char direction,char offset,char kase)
  1070. #else
  1071. long find_string(start_line,start_col,needle,len,direction,offset,kase)
  1072. long start_line;
  1073. short *start_col;
  1074. char *needle;
  1075. short len;
  1076. char direction;
  1077. char offset;
  1078. char kase;
  1079. #endif
  1080. /***********************************************************************/
  1081. /* Finds a string from the line after/before the current line/col      */
  1082. /* containing needle. Zone settings are considered as is case.         */
  1083. /* PARAMETERS:                                                         */
  1084. /* start_line - current line number                                    */
  1085. /* start_col  - current column-offset from very beginning of buffer    */
  1086. /* needle     - string to search for                                   */
  1087. /* len        - length of needle (could contain nulls)                 */
  1088. /* direction  - forward or backward                                    */
  1089. /*              value of 1 if forward, -1 if backward                  */
  1090. /* offset     - offset from current line. For searches, this is the    */
  1091. /*              same as direction. For changes, this is 0. ie look at  */
  1092. /*              the current line before going on to the next/prev line.*/
  1093. /* kase       - indicates if case is to be considered when matching    */
  1094. /* RETURN:    - TARGET_NOT_FOUND if not found                          */
  1095. /*            - line number if found and column                        */
  1096. /***********************************************************************/
  1097. {
  1098. /*--------------------------- local data ------------------------------*/
  1099.  LINE *curr;
  1100.  long i;
  1101.  short real_start,real_end,loc;
  1102.  bool use_rec=FALSE;
  1103.  unsigned char *ptr;
  1104.  unsigned short haystack_len;
  1105. /*--------------------------- processing ------------------------------*/
  1106. #ifdef TRACE
  1107.  trace_function("util.c:    find_string");
  1108. #endif
  1109.  
  1110.  post_process_line(CURRENT_VIEW->focus_line);
  1111.  if (len == 0)
  1112.     use_rec = TRUE;
  1113.  else
  1114.     if (*(needle+(len-1)) == ' ')
  1115.        use_rec = TRUE;
  1116.  curr = ll_find(CURRENT_FILE->first_line,start_line+offset);
  1117.  for (i=0;;i+=(long)direction)
  1118.    {
  1119.     if ((curr->next == NULL && direction == DIRECTION_FORWARD)
  1120.     ||  (curr->prev == NULL && direction == DIRECTION_BACKWARD))
  1121.       break;
  1122.     if (use_rec)
  1123.       {
  1124.        memset(rec,' ',MAX_LINE_LENGTH);
  1125.        memcpy(rec,curr->line,curr->length);
  1126.        ptr = rec;
  1127.        haystack_len = MAX_LINE_LENGTH;
  1128.       }
  1129.     else
  1130.       {
  1131.        ptr = curr->line;
  1132.        haystack_len = curr->length;
  1133.       }
  1134.     real_end = min(haystack_len,CURRENT_VIEW->zone_end-1);
  1135.     real_start = max(*start_col,CURRENT_VIEW->zone_start-1);
  1136.  
  1137.     if (kase == CASE_IGNORE)
  1138.        loc = memposi(ptr+real_start,
  1139.                     (real_end - real_start + 1),needle,len);
  1140.     else
  1141.        loc = mempos(ptr+real_start,
  1142.                     (real_end - real_start + 1),needle,len);
  1143.     if (loc != (-1))
  1144.       {
  1145.        *start_col = loc+real_start;
  1146.        pre_process_line(CURRENT_VIEW->focus_line);
  1147. #ifdef TRACE
  1148.        trace_return();
  1149. #endif
  1150.        return(i+offset);
  1151.       }
  1152.  
  1153. /*---------------------------------------------------------------------*/
  1154. /* Once we get here, we have gone on to a new line. For searches, the  */
  1155. /* start_col has to be set back to 0 so that it can look from the      */
  1156. /* beginning of a new line.                                            */
  1157. /*---------------------------------------------------------------------*/
  1158.     *start_col = 0;
  1159.     if (direction == DIRECTION_FORWARD)
  1160.        curr = curr->next;
  1161.     else
  1162.        curr = curr->prev;
  1163.    }
  1164. #ifdef TRACE
  1165.  trace_return();
  1166. #endif
  1167.  pre_process_line(CURRENT_VIEW->focus_line);
  1168.  return(TARGET_NOT_FOUND);
  1169. }
  1170. /***********************************************************************/
  1171. #ifdef PROTO
  1172. void put_char(WINDOW *win,chtype ch,char add_ins)
  1173. #else
  1174. void put_char(win,ch,add_ins)
  1175. WINDOW *win;
  1176. chtype ch;
  1177. char add_ins;
  1178. #endif
  1179. /***********************************************************************/
  1180. {
  1181. /*--------------------------- local data ------------------------------*/
  1182.  chtype chr;
  1183. /*--------------------------- processing ------------------------------*/
  1184. #ifdef TRACE
  1185.  trace_function("util.c:    put_char");
  1186. #endif
  1187.  chr = ch;
  1188. #if !defined(DOS) && !defined(OS2)
  1189.  if (chr < ' ')
  1190.     chr = ('@' + chr) | A_REVERSE;
  1191. #endif
  1192.  if (add_ins == ADDCHAR)
  1193.     waddch(win,chr);
  1194.  else
  1195.     winsch(win,chr);
  1196. #ifdef TRACE
  1197.  trace_return();
  1198. #endif
  1199.  return;
  1200. }
  1201. /***********************************************************************/
  1202. #ifdef PROTO
  1203. void set_up_windows(void)
  1204. #else
  1205. void set_up_windows()
  1206. #endif
  1207. /***********************************************************************/
  1208. {
  1209. /*--------------------------- local data ------------------------------*/
  1210.  register short i;
  1211.  unsigned char command_location;
  1212.  extern short file_start;
  1213.  extern unsigned char *dirfilename;
  1214. /*--------------------------- processing ------------------------------*/
  1215. #ifdef TRACE
  1216.  trace_function("util.c:    set_up_windows");
  1217. #endif
  1218.  if (CURRENT_VIEW->cmd_line == 'T')   /* command top */
  1219.    {
  1220.     CURRENT_VIEW->command_row = CURRENT_SCREEN.origin_y+1;
  1221.     command_location = 1;
  1222.    }
  1223.  else
  1224.    {
  1225.     CURRENT_VIEW->command_row = CURRENT_SCREEN.origin_y+CURRENT_SCREEN.screen_rows-1;
  1226.     command_location = 0;
  1227.    }
  1228.  
  1229.  CURRENT_SCREEN.rows = CURRENT_SCREEN.screen_rows-2;
  1230.  CURRENT_SCREEN.top_row = CURRENT_SCREEN.origin_y+1+command_location;
  1231.  if (CURRENT_VIEW->prefix_on)
  1232.    {
  1233.     CURRENT_SCREEN.cols = CURRENT_SCREEN.screen_cols-PREFIX_WIDTH;
  1234.     if (CURRENT_VIEW->prefix_left)
  1235.       {
  1236.        CURRENT_SCREEN.top_col = CURRENT_SCREEN.origin_x+PREFIX_WIDTH;
  1237.        CURRENT_WINDOW_PREFIX = newwin(CURRENT_SCREEN.rows,PREFIX_WIDTH,
  1238.                                  CURRENT_SCREEN.top_row,CURRENT_SCREEN.origin_x);
  1239.       }
  1240.     else
  1241.       {
  1242.        CURRENT_SCREEN.top_col = CURRENT_SCREEN.origin_x;
  1243.        CURRENT_WINDOW_PREFIX = newwin(CURRENT_SCREEN.rows,PREFIX_WIDTH,
  1244.                                  CURRENT_SCREEN.top_row,
  1245.                                  CURRENT_SCREEN.origin_x+CURRENT_SCREEN.cols);
  1246.       }
  1247.    }
  1248.  else
  1249.    {
  1250.     CURRENT_SCREEN.cols = CURRENT_SCREEN.screen_cols;
  1251.     CURRENT_SCREEN.top_col = CURRENT_SCREEN.origin_x;
  1252.    }
  1253.  CURRENT_WINDOW_MAIN = newwin(CURRENT_SCREEN.rows,
  1254.                                CURRENT_SCREEN.cols,
  1255.                                CURRENT_SCREEN.top_row,
  1256.                                CURRENT_SCREEN.top_col);
  1257.  CURRENT_WINDOW_COMMAND = newwin(1,CURRENT_SCREEN.screen_cols-PREFIX_WIDTH,
  1258.                                    CURRENT_VIEW->command_row,
  1259.                                    CURRENT_SCREEN.origin_x+PREFIX_WIDTH);
  1260.  CURRENT_WINDOW_ARROW = newwin(1,PREFIX_WIDTH,
  1261.                                CURRENT_VIEW->command_row,
  1262.                                CURRENT_SCREEN.origin_x);
  1263.  CURRENT_WINDOW_IDLINE = newwin(1,CURRENT_SCREEN.screen_cols,
  1264.                                   CURRENT_SCREEN.origin_y,
  1265.                                   CURRENT_SCREEN.origin_x);
  1266.  wattrset(CURRENT_WINDOW_ARROW,colour[ATTR_ARROW]);
  1267.  wattrset(CURRENT_WINDOW_MAIN,colour[ATTR_FILEAREA]);
  1268.  wattrset(CURRENT_WINDOW_IDLINE,colour[ATTR_IDLINE]);
  1269.  for (i=0;i<CURRENT_SCREEN.screen_cols;i++)
  1270.      mvwaddch(CURRENT_WINDOW_IDLINE,0,i,' ');
  1271. #ifdef SUN
  1272.  notimeout(CURRENT_WINDOW_MAIN,TRUE);
  1273.  notimeout(CURRENT_WINDOW_COMMAND,TRUE);
  1274. #endif
  1275.  wattrset(CURRENT_WINDOW_COMMAND,colour[ATTR_CMDLINE]);
  1276.  wmove(CURRENT_WINDOW_COMMAND,0,0);
  1277.  wclrtoeol(CURRENT_WINDOW_COMMAND);
  1278.  if (CURRENT_VIEW->prefix_on)
  1279.    {
  1280. #ifdef SUN
  1281.     notimeout(CURRENT_WINDOW_PREFIX,TRUE);
  1282. #endif
  1283.     wattrset(CURRENT_WINDOW_PREFIX,colour[ATTR_PREFIX]);
  1284. #if !defined(SUN) && !defined(VMS)
  1285.     keypad(CURRENT_WINDOW_PREFIX,TRUE);
  1286. #endif
  1287.    }
  1288. #if !defined(SUN) && !defined(VMS)
  1289.  keypad(CURRENT_WINDOW_COMMAND,TRUE);
  1290.  keypad(CURRENT_WINDOW_MAIN,TRUE);
  1291. #endif
  1292.  CURRENT_VIEW->current_window = WINDOW_COMMAND;
  1293.  CURRENT_VIEW->previous_window = WINDOW_MAIN;
  1294.  mvwaddstr(CURRENT_WINDOW_ARROW,0,0,"====> ");
  1295.  wnoutrefresh(CURRENT_WINDOW_ARROW);
  1296.  wnoutrefresh(CURRENT_WINDOW_COMMAND);
  1297.  if (strcmp(CURRENT_FILE->fname,dirfilename) == 0)
  1298.    {
  1299.     CURRENT_VIEW->focus_line++;
  1300.     wmove(CURRENT_WINDOW_MAIN,CURRENT_VIEW->current_row,file_start-1);
  1301.    }
  1302.  wmove(CURRENT_WINDOW_COMMAND,0,0);
  1303. #ifdef TRACE
  1304.  trace_return();
  1305. #endif
  1306.  return;
  1307. }
  1308. /***********************************************************************/
  1309. #ifdef PROTO
  1310. void delete_windows(void)
  1311. #else
  1312. void delete_windows()
  1313. #endif
  1314. /***********************************************************************/
  1315. {
  1316. /*--------------------------- local data ------------------------------*/
  1317. /*--------------------------- processing ------------------------------*/
  1318. #ifdef TRACE
  1319.  trace_function("util.c:    delete_windows");
  1320. #endif
  1321.  
  1322.  if (CURRENT_VIEW->prefix_on)
  1323.     delwin(CURRENT_WINDOW_PREFIX);
  1324.  delwin(CURRENT_WINDOW_MAIN);
  1325.  delwin(CURRENT_WINDOW_COMMAND);
  1326.  delwin(CURRENT_WINDOW_IDLINE);
  1327.  delwin(CURRENT_WINDOW_ARROW);
  1328.  
  1329. #ifdef TRACE
  1330.  trace_return();
  1331. #endif
  1332.  return;
  1333. }
  1334. /***********************************************************************/
  1335. #ifdef PROTO
  1336. void pre_process_line(long line_number)
  1337. #else
  1338. void pre_process_line(line_number)
  1339. long line_number;
  1340. #endif
  1341. /***********************************************************************/
  1342. {
  1343. /*--------------------------- local data ------------------------------*/
  1344.  LINE *curr;
  1345. /*--------------------------- processing ------------------------------*/
  1346. #ifdef TRACE
  1347.  trace_function("util.c:    pre_process_line");
  1348. #endif
  1349.  curr = ll_find(CURRENT_FILE->first_line,line_number);
  1350.  memset(rec,' ',MAX_LINE_LENGTH);
  1351.  memcpy(rec,curr->line,curr->length);
  1352.  rec_len = curr->length;
  1353. #ifdef TRACE
  1354.  trace_return();
  1355. #endif
  1356.  return;
  1357. }
  1358. /***********************************************************************/
  1359. #ifdef PROTO
  1360. void post_process_line(long line_number)
  1361. #else
  1362. void post_process_line(line_number)
  1363. long line_number;
  1364. #endif
  1365. /***********************************************************************/
  1366. {
  1367. /*--------------------------- local data ------------------------------*/
  1368.  LINE *curr;
  1369. /*--------------------------- processing ------------------------------*/
  1370. #ifdef TRACE
  1371.  trace_function("util.c:    post_process_line");
  1372. #endif
  1373. /*---------------------------------------------------------------------*/
  1374. /* Find the specified line in the linked list...                       */
  1375. /*---------------------------------------------------------------------*/
  1376.  curr = ll_find(CURRENT_FILE->first_line,line_number);
  1377. /*---------------------------------------------------------------------*/
  1378. /* If the line hasn't changed, return.                                 */
  1379. /*---------------------------------------------------------------------*/
  1380.  if (rec_len == curr->length && (memcmp(rec,curr->line,curr->length) == 0))
  1381.    {
  1382. #ifdef TRACE
  1383.     trace_return();
  1384. #endif
  1385.     return;
  1386.    }
  1387. /*---------------------------------------------------------------------*/
  1388. /* Increment the alteration counters...                                */
  1389. /*---------------------------------------------------------------------*/
  1390.  increment_alt();
  1391. /*---------------------------------------------------------------------*/
  1392. /* Add the old line contents to the line recovery list.                */
  1393. /*---------------------------------------------------------------------*/
  1394.  add_to_recovery_list(curr->line,curr->length);
  1395. /*---------------------------------------------------------------------*/
  1396. /* Realloc the dynamic memory for the line if the line is now longer.  */
  1397. /*---------------------------------------------------------------------*/
  1398.  if (rec_len > curr->length)
  1399.                               /* what if realloc fails ?? */
  1400.     curr->line = (unsigned char *)memRealloc((void *)curr->line,(rec_len+1)*sizeof(unsigned char),
  1401.                   "post_process_line realloc");
  1402.  
  1403. /*---------------------------------------------------------------------*/
  1404. /* Copy the contents of rec into the line.                             */
  1405. /*---------------------------------------------------------------------*/
  1406.  memcpy(curr->line,rec,rec_len);
  1407.  curr->length = rec_len;
  1408.  *(curr->line+rec_len) = '\0';
  1409. #ifdef TRACE
  1410.  trace_return();
  1411. #endif
  1412.  return;
  1413. }
  1414. /***********************************************************************/
  1415. #ifdef PROTO
  1416. short blank_field(unsigned char *field)
  1417. #else
  1418. short blank_field(field)
  1419. unsigned char *field;
  1420. #endif
  1421. /***********************************************************************/
  1422. {
  1423. /*--------------------------- local data ------------------------------*/
  1424. /*--------------------------- processing ------------------------------*/
  1425. #ifdef TRACE
  1426.  trace_function("util.c:    blank_field");
  1427. #endif
  1428.  if (strzne(field,' ') == (-1))
  1429.    {
  1430. #ifdef TRACE
  1431.     trace_return();
  1432. #endif
  1433.     return(TRUE);                /* field is NULL or just contains spaces */
  1434.    }
  1435. #ifdef TRACE
  1436.  trace_return();
  1437. #endif
  1438.  return(FALSE);
  1439. }
  1440. /***********************************************************************/
  1441. #ifdef PROTO
  1442. void adjust_marked_block(char insert_line,long base_line,long num_lines)
  1443. #else
  1444. void adjust_marked_block(insert_line,base_line,num_lines)
  1445. char insert_line;
  1446. long base_line;
  1447. long num_lines;
  1448. #endif
  1449. /***********************************************************************/
  1450. {
  1451. /*---------------------------------------------------------------------*/
  1452. /* When lines are deleted, the base line is the first line in the file */
  1453. /* irrespective of the direction that the delete is done.              */
  1454. /*---------------------------------------------------------------------*/
  1455. /*--------------------------- local data ------------------------------*/
  1456. /*--------------------------- processing ------------------------------*/
  1457. #ifdef TRACE
  1458.  trace_function("util.c:    adjust_marked_block");
  1459. #endif
  1460.  if (MARK_VIEW != CURRENT_VIEW)
  1461.    {
  1462. #ifdef TRACE
  1463.     trace_return();
  1464. #endif
  1465.     return;
  1466.    }
  1467.  switch(insert_line)
  1468.   {
  1469.    case YES:/* INSERT */
  1470.             if (base_line < CURRENT_VIEW->mark_start_line)
  1471.               {
  1472.                CURRENT_VIEW->mark_start_line += num_lines;
  1473.                CURRENT_VIEW->mark_end_line += num_lines;
  1474.                break;
  1475.               }
  1476.             if (base_line >= CURRENT_VIEW->mark_start_line
  1477.             &&  base_line < CURRENT_VIEW->mark_end_line)
  1478.               {
  1479.                CURRENT_VIEW->mark_end_line += num_lines;
  1480.                break;
  1481.               }
  1482.             break;
  1483.   case NO:  /* DELETE */
  1484.             if (base_line <= CURRENT_VIEW->mark_start_line
  1485.             &&  base_line+num_lines >= CURRENT_VIEW->mark_end_line)
  1486.               {
  1487.                CURRENT_VIEW->mark_start_line = (-1);
  1488.                CURRENT_VIEW->mark_end_line = (-1);
  1489.                MARK_VIEW = (VIEW_DETAILS *)NULL;
  1490.                break;
  1491.               }
  1492.             if (base_line+num_lines < CURRENT_VIEW->mark_start_line)
  1493.               {
  1494.                CURRENT_VIEW->mark_start_line -= num_lines;
  1495.                CURRENT_VIEW->mark_end_line -= num_lines;
  1496.                break;
  1497.               }
  1498.             if (base_line > CURRENT_VIEW->mark_end_line)
  1499.               {
  1500.                break;
  1501.               }
  1502.             if (base_line+num_lines > CURRENT_VIEW->mark_end_line)
  1503.               {
  1504.                CURRENT_VIEW->mark_end_line = base_line - 1;
  1505.                break;
  1506.               }
  1507.             CURRENT_VIEW->mark_end_line -= num_lines;
  1508.  
  1509.             break;
  1510.   }
  1511.  return;
  1512. #ifdef TRACE
  1513.  trace_return();
  1514. #endif
  1515. }
  1516. /***********************************************************************/
  1517. #ifdef PROTO
  1518. char case_translate(char key)
  1519. #else
  1520. char case_translate(key)
  1521. char key;
  1522. #endif
  1523. /***********************************************************************/
  1524. {
  1525. /*--------------------------- local data ------------------------------*/
  1526. /*--------------------------- processing ------------------------------*/
  1527. #ifdef TRACE
  1528.  trace_function("util.c:    case_translate");
  1529. #endif
  1530.  if (CURRENT_VIEW->case_enter == CASE_UPPER
  1531.  && islower(key))
  1532.    {
  1533. #ifdef TRACE
  1534.     trace_return();
  1535. #endif
  1536.     return(toupper(key));
  1537.    }
  1538.  if (CURRENT_VIEW->case_enter == CASE_LOWER
  1539.  && isupper(key))
  1540.    {
  1541. #ifdef TRACE
  1542.     trace_return();
  1543. #endif
  1544.     return(tolower(key));
  1545.    }
  1546. #ifdef TRACE
  1547.  trace_return();
  1548. #endif
  1549.  return(key);
  1550. }
  1551. /***********************************************************************/
  1552. #ifdef PROTO
  1553. void add_to_recovery_list(char *line,int len)
  1554. #else
  1555. void add_to_recovery_list(line,len)
  1556. char *line;
  1557. int len;
  1558. #endif
  1559. /***********************************************************************/
  1560. {
  1561. /*--------------------------- local data ------------------------------*/
  1562.  register int i;
  1563. /*--------------------------- processing ------------------------------*/
  1564. #ifdef TRACE
  1565.  trace_function("util.c:    add_to_recovery_list");
  1566. #endif
  1567. /*---------------------------------------------------------------------*/
  1568. /* Ignore empty lines.                                                 */
  1569. /*---------------------------------------------------------------------*/
  1570.  if (len == 0)                          
  1571.    {
  1572. #ifdef TRACE
  1573.     trace_return();
  1574. #endif
  1575.     return;
  1576.    }
  1577. /*---------------------------------------------------------------------*/
  1578. /* First time through, set length array to (-1) to indicated unused.   */
  1579. /* This setup MUST occur before the freeing up code.                   */
  1580. /*---------------------------------------------------------------------*/
  1581.  if (add_recv == (-1))
  1582.    {
  1583.     for (i=0;i<MAX_RECV;i++)
  1584.        recv_len[i] = (-1);
  1585.     add_recv = 0;               /* set to point to next available slot */
  1586.    }
  1587. /*---------------------------------------------------------------------*/
  1588. /* Special case at end of program to free up dynamic memory allocated. */
  1589. /*---------------------------------------------------------------------*/
  1590.  if (len == (-1) && line == NULL)
  1591.    {
  1592.     for (i=0;i<MAX_RECV;i++)
  1593.        {
  1594.         if (recv_len[i] != (-1))
  1595.            free(recv[i]);
  1596.        }
  1597. #ifdef TRACE
  1598.     trace_return();
  1599. #endif
  1600.     return;
  1601.    }
  1602. /*---------------------------------------------------------------------*/
  1603. /* Now we are here, lets add to the array.                             */
  1604. /*---------------------------------------------------------------------*/
  1605.  if (recv_len[add_recv] == (-1))  /* haven't malloced yet */
  1606.    {
  1607.     if ((recv[add_recv] = (unsigned char *)memMalloc((len+1)*sizeof(char),"add_to_recovery")) == NULL)
  1608.       {
  1609.        display_error(30,(unsigned char *)"");
  1610. #ifdef TRACE
  1611.        trace_return();
  1612. #endif
  1613.        return;
  1614.       }
  1615.    }
  1616.  else
  1617.    {
  1618.     if ((recv[add_recv] = (unsigned char *)memRealloc(recv[add_recv],(len+1)*sizeof(char),"add to recovery")) == NULL)
  1619.       {
  1620.        display_error(30,(unsigned char *)"");
  1621. #ifdef TRACE
  1622.        trace_return();
  1623. #endif
  1624.        return;
  1625.       }
  1626.    }
  1627.  memcpy(recv[add_recv],line,len);
  1628.  recv_len[add_recv] = len;
  1629.  retr_recv = add_recv;
  1630.  add_recv = (++add_recv >= MAX_RECV) ? 0 : add_recv;
  1631.  num_recv = (++num_recv > MAX_RECV) ? MAX_RECV : num_recv;
  1632.  
  1633. #ifdef TRACE
  1634.  trace_return();
  1635. #endif
  1636.  return;
  1637. }
  1638. /***********************************************************************/
  1639. #ifdef PROTO
  1640. void get_from_recovery_list(int num)
  1641. #else
  1642. void get_from_recovery_list(num)
  1643. int num;
  1644. #endif
  1645. /***********************************************************************/
  1646. {
  1647. /*--------------------------- local data ------------------------------*/
  1648.  register int i;
  1649.  int num_retr = min(num,num_recv);
  1650. /*--------------------------- processing ------------------------------*/
  1651. #ifdef TRACE
  1652.  trace_function("util.c:    get_from_recovery_list");
  1653. #endif
  1654. /*---------------------------------------------------------------------*/
  1655. /* Return error if nothing to recover.                                 */
  1656. /*---------------------------------------------------------------------*/
  1657.  if (retr_recv == (-1))
  1658.    {
  1659.     display_error(0,(unsigned char *)"0 line(s) recovered");
  1660. #ifdef TRACE
  1661.     trace_return();
  1662. #endif
  1663.     return;
  1664.    }
  1665. /*---------------------------------------------------------------------*/
  1666. /* Retrieve each allocated recovery line and put back into the body.   */
  1667. /*---------------------------------------------------------------------*/
  1668.  
  1669.  for (i=0;i<num_retr;i++)
  1670.    {
  1671.     if (recv_len[retr_recv] != (-1))
  1672.       {
  1673.        insert_new_line(recv[retr_recv],recv_len[retr_recv],1L,TRUE);
  1674.        retr_recv = (--retr_recv < 0) ? num_recv-1 : retr_recv;
  1675.       }
  1676.    }
  1677.  
  1678.  sprintf(temp_cmd,"%d line(s) recovered",num_retr);
  1679.  display_error(0,(unsigned char *)temp_cmd);
  1680. #ifdef TRACE
  1681.  trace_return();
  1682. #endif
  1683.  return;
  1684. }
  1685. #ifdef DOS
  1686. /***********************************************************************/
  1687. void draw_cursor(void)
  1688. /***********************************************************************/
  1689. {
  1690. /*--------------------------- local data ------------------------------*/
  1691. /*--------------------------- processing ------------------------------*/
  1692. #ifdef TRACE
  1693.  trace_function("util.c:    draw_cursor");
  1694. #endif
  1695.  if (mode_insert)
  1696.     if (get_mode() == 7)
  1697.        csr_size(5,13);
  1698.     else
  1699.        csr_size(3,7);
  1700.  else
  1701.     if (get_mode() == 7)
  1702.        csr_size(12,13);
  1703.     else
  1704.        csr_size(6,7);
  1705. #ifdef TRACE
  1706.  trace_return();
  1707. #endif
  1708.  return;
  1709. }
  1710. #endif
  1711.  
  1712. #ifdef OS2
  1713. /***********************************************************************/
  1714. void draw_cursor(void)
  1715. /***********************************************************************/
  1716. {
  1717. /*--------------------------- local data ------------------------------*/
  1718. /*--------------------------- processing ------------------------------*/
  1719. #ifdef TRACE
  1720.  trace_function("util.c:    draw_cursor");
  1721. #endif
  1722.  if (mode_insert)
  1723.     _set_cursor_mode(5,13);
  1724.  else
  1725.     _set_cursor_mode(12,13);
  1726. #ifdef TRACE
  1727.  trace_return();
  1728. #endif
  1729.  return;
  1730. }
  1731. #endif
  1732.